/*!
    \file    bsp_uart.c
    \brief   串口板级驱动
    \author : caizhiwei
    \version 2023-02-26, V1.0.0
*/

#include "bsp_uart.h"

stUARTx RS485_1 = {.uartx_periph = COM_RS485_1,};  
stUARTx RS485_2 = {.uartx_periph = COM_RS485_2,};

stUARTx RS232_Debug = {.uartx_periph = COM_RS232_Debug,};
stUARTx RS232_LCD = {.uartx_periph = COM_RS232_LCD,};
stUARTx RS232_NFC = {.uartx_periph = COM_RS232_NFC,};

stUARTx Uart_4G = {.uartx_periph = COM_4G,};


/*!
    \brief      PA9--TX  PA10--RX (CMU -> RS485_1)
    \param[in]  baudrate: 
    \param[out] none
    \retval     none
*/
void BSP_Uart0_Config(uint32_t baudrate)
{
	rcu_periph_clock_enable(RCU_GPIOA);
	/* enable USART clock */
	rcu_periph_clock_enable(RCU_USART0);

	/* connect port to USARTx_Tx */
	gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9|GPIO_PIN_10);

	/* configure USART Tx RX as alternate function push-pull */
	gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9|GPIO_PIN_10);
	gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9|GPIO_PIN_10);

	/* USART configure */
	usart_baudrate_set(USART0,baudrate);
	usart_receive_config(USART0, USART_RECEIVE_ENABLE);
	usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);

	usart_deinit(USART0);	
	
	//usart_sample_bit_config(com, USART_OSB_1bit);
	usart_oversample_config(USART0, USART_OVSMOD_8);
	usart_baudrate_set(USART0,baudrate); 			//波特率
	usart_parity_config(USART0, USART_PM_NONE);		//校验位:NONE
	usart_word_length_set(USART0, USART_WL_8BIT);	//数据位:8
	usart_stop_bit_set(USART0, USART_STB_1BIT);		//停止位:1
	usart_receive_config(USART0, USART_RECEIVE_ENABLE);
	usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);

	//usart_dma_transmit_config(USART0, USART_DENT_ENABLE);  //打开串口DMA发送
	//usart_dma_receive_config(USART0, USART_DENR_ENABLE);   //打开串口DMA接收

	usart_flag_clear(USART0, USART_FLAG_TC);

	/* enable USART0 transmit interrupt */
	//usart_interrupt_enable(USART0, USART_INT_TBE);
	
	/* enable UARTx receive interrupt */
	usart_interrupt_enable(USART0, USART_INT_RBNE);
	/* enable USARTx IDLEIE interrupt */
	usart_interrupt_enable(USART0, USART_INT_IDLE);

	/* USART interrupt configuration */
	nvic_irq_enable(USART0_IRQn, 1, 0);
	
	usart_enable(USART0);
}


void USART0_IRQHandler(void)
{
	//若接收到一个字节
	if( RESET != usart_flag_get(USART0, USART_FLAG_RBNE))
    { 
        RS485_1.uart_rx.buf[RS485_1.uart_rx.cnt++] = (uint8_t)usart_data_receive(USART0);
    }
	//若接收检测到空闲帧中断
	if(RESET != usart_flag_get(USART0, USART_FLAG_IDLE))
	{
		usart_data_receive(USART0); // 读寄存器自动清除标志
		RS485_1.uart_rx.idle_flg = true;	
    }
}


/*!
    \brief      PA2--TX  PA3--RX (RS232 -> NFC)
    \param[in]  baudrate: 
    \param[out] none
    \retval     none
*/
void BSP_Uart1_Config(uint32_t baudrate)
{
	/* enable gpio USART1 clock */
    rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_USART1);

	/* connect port to USARTx_Tx */
	gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_2 | GPIO_PIN_3);

	/* configure USART Tx Rx as alternate function push-pull */
	gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2 | GPIO_PIN_3);
	gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2 | GPIO_PIN_3);

	/* USART configure */
	//usart_sample_bit_config(USART1, USART_OSB_1bit);
	usart_oversample_config(USART1, USART_OVSMOD_8);
	usart_baudrate_set(USART1,baudrate); 			//波特率
	usart_parity_config(USART1, USART_PM_NONE);		//校验位:NONE
	usart_word_length_set(USART1, USART_WL_8BIT);	//数据位:8
	usart_stop_bit_set(USART1, USART_STB_1BIT);		//停止位:1
	
	usart_receive_config(USART1, USART_RECEIVE_ENABLE);
	usart_transmit_config(USART1, USART_TRANSMIT_ENABLE);

	//usart_dma_transmit_config(USART1, USART_DENT_ENABLE);//打开串口DMA发送
	//usart_dma_receive_config(USART1, USART_DENR_ENABLE);//打开串口DMA接收
	
	usart_flag_clear(USART1, USART_FLAG_TC);

	/* enable UARTx 发送中断 */
	//usart_interrupt_enable(USART1, USART_INT_TBE);
	
	/* enable UARTx receive interrupt */
	usart_interrupt_enable(USART1, USART_INT_RBNE);
	/* enable USARTx IDLEIE 空闲中断*/
	usart_interrupt_enable(USART1, USART_INT_IDLE);

	/* USART interrupt configuration */
	nvic_irq_enable(USART1_IRQn, 2, 0);

	usart_enable(USART1);

}


void USART1_IRQHandler(void)
{
	//若接收到一个字节
	if( RESET != usart_flag_get(USART1, USART_FLAG_RBNE))
    { 
       RS232_NFC.uart_rx.buf[RS232_NFC.uart_rx.cnt++] = usart_data_receive(USART1);    
    }
	//若接收检测到空闲帧中断
	if(RESET != usart_flag_get(USART1, USART_FLAG_IDLE))
	{
		usart_data_receive(USART1); // 读寄存器自动清除标志	
		RS232_NFC.uart_rx.idle_flg = true;
    }
}


/*!
    \brief      PD8--TX  PD9--RX (CMU -> RS485_2)
    \param[in]  baudrate: 
    \param[out] none
    \retval     none
*/
void BSP_Uart2_Config(uint32_t baudrate)
{
	/* enable gpio USART2 clock */
    rcu_periph_clock_enable(RCU_GPIOD);
	rcu_periph_clock_enable(RCU_USART2);

	/* connect port to USARTx_Tx Rx */
	gpio_af_set(GPIOD, GPIO_AF_7, GPIO_PIN_8 | GPIO_PIN_9 );

	/* configure USART Tx Rx as alternate function push-pull */
	gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_8 | GPIO_PIN_9);
	gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8 | GPIO_PIN_9);

	/* USART configure */
	//usart_sample_bit_config(USART1, USART_OSB_1bit);
	usart_oversample_config(USART2, USART_OVSMOD_8);
	usart_baudrate_set(USART2,baudrate);			//波特率
	usart_parity_config(USART2, USART_PM_NONE); 	//校验位:NONE
	usart_word_length_set(USART2, USART_WL_8BIT);	//数据位:8
	usart_stop_bit_set(USART2, USART_STB_1BIT); 	//停止位:1
	
	usart_receive_config(USART2, USART_RECEIVE_ENABLE);
	usart_transmit_config(USART2, USART_TRANSMIT_ENABLE);

	//usart_dma_transmit_config(USART2, USART_DENT_ENABLE);//打开串口DMA发送
	//usart_dma_receive_config(USART2, USART_DENR_ENABLE);//打开串口DMA接收
	
	usart_flag_clear(USART2, USART_FLAG_TC);

	/* enable UARTx 发送中断 */
	//usart_interrupt_enable(USART2, USART_INT_TBE);
	
	/* enable UARTx receive interrupt */
	usart_interrupt_enable(USART2, USART_INT_RBNE);
	
	/* enable USARTx IDLEIE 空闲中断*/
	usart_interrupt_enable(USART2, USART_INT_IDLE);

	/* USART interrupt configuration */
	nvic_irq_enable(USART2_IRQn, 3, 0);
	
	usart_enable(USART2);
}


void USART2_IRQHandler(void)
{
	//若接收到一个字节
	if( RESET != usart_flag_get(USART2, USART_FLAG_RBNE))
    { 
       RS485_2.uart_rx.buf[RS485_2.uart_rx.cnt++] = usart_data_receive(USART2);    
    }
	//若接收检测到空闲帧中断
	if(RESET != usart_flag_get(USART2, USART_FLAG_IDLE))
	{
		usart_data_receive(USART2); // 读寄存器自动清除标志	
		RS485_2.uart_rx.idle_flg = true;
    }
}

/*!
    \brief      PA0--TX  PA1--RX (CMU -> RS232_2 -> DEBUG com)
    \param[in]  baudrate: 
    \param[out] none
    \retval     none
*/
void BSP_Uart3_Config(uint32_t baudrate)
{
	/* enable gpio USART3 clock */
    rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_UART3);

	/* connect port to USARTx_Tx Rx */
	gpio_af_set(GPIOA, GPIO_AF_8, GPIO_PIN_0 | GPIO_PIN_1);

	/* configure USART Tx Rx as alternate function push-pull */
	gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_0 | GPIO_PIN_1);
	gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0 | GPIO_PIN_1);

	/* USART configure */
	//usart_sample_bit_config(UART3, USART_OSB_1bit);
	usart_oversample_config(UART3, USART_OVSMOD_8);
	usart_baudrate_set(UART3,baudrate);			//波特率
	usart_parity_config(UART3, USART_PM_NONE); 	//校验位:NONE
	usart_word_length_set(UART3, USART_WL_8BIT);//数据位:8
	usart_stop_bit_set(UART3, USART_STB_1BIT); 	//停止位:1
	
	usart_receive_config(UART3, USART_RECEIVE_ENABLE);
	usart_transmit_config(UART3, USART_TRANSMIT_ENABLE);

	//usart_dma_transmit_config(USART1, USART_DENT_ENABLE);//打开串口DMA发送
	//usart_dma_receive_config(USART1, USART_DENR_ENABLE);//打开串口DMA接收
	
	usart_flag_clear(UART3, USART_FLAG_TC);

	/* enable UARTx 发送中断 */
	//usart_interrupt_enable(UART3, USART_INT_TBE);
	
	/* enable UARTx receive interrupt */
	//usart_interrupt_enable(UART3, USART_INT_RBNE);
	
	/* enable USARTx IDLEIE 空闲中断*/
	usart_interrupt_enable(UART3, USART_INT_IDLE);

	/* USART interrupt configuration */
	nvic_irq_enable(UART3_IRQn, 4, 0);
	
	usart_enable(UART3);
}


void UART3_IRQHandler(void)
{
	uint8_t ch = 0;
	//若接收到一个字节
	if( RESET != usart_flag_get(UART3, USART_FLAG_RBNE))
	{ 
	   ch = usart_data_receive(UART3);   
	   
	}
}



/* For CMU 4G */
void BSP_Uart5_Config(uint32_t baudrate)
{
	/* enable gpio USART5 clock */
    rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_USART5);

	/* connect port to USART5_Tx Rx*/
	gpio_af_set(GPIOC, GPIO_AF_8, GPIO_PIN_7 | GPIO_PIN_6);

	/* configure USART Tx Rx as alternate function push-pull */
	gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7|GPIO_PIN_6);
	gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7|GPIO_PIN_6);

	usart_deinit(USART5);
	//usart_sample_bit_config(com, USART_OSB_1bit);
	usart_oversample_config(USART5, USART_OVSMOD_8);
	usart_baudrate_set(USART5,baudrate);			//波特率
	usart_parity_config(USART5, USART_PM_NONE);		//校验位:NONE
	usart_word_length_set(USART5, USART_WL_8BIT);	//数据位:8
	usart_stop_bit_set(USART5, USART_STB_1BIT);		//停止位:1
	usart_receive_config(USART5, USART_RECEIVE_ENABLE);
	usart_transmit_config(USART5, USART_TRANSMIT_ENABLE);
	usart_enable(USART5);
	
    /* enable UART5 transmit interrupt */
    //usart_interrupt_enable(USART5, USART_INT_TBE);
    
	/* enable UARTx receive interrupt */
	usart_interrupt_enable(USART5, USART_INT_RBNE);
	/* enable USARTx IDLEIE interrupt */
	usart_interrupt_enable(USART5, USART_INT_IDLE);

	/* USART interrupt configuration */
	nvic_irq_enable(USART5_IRQn, 5, 0);
	   
}



void USART5_IRQHandler(void)
{
	//若接收到一个字节
	if( RESET != usart_flag_get(USART5, USART_FLAG_RBNE))
    { 
       Uart_4G.uart_rx.buf[Uart_4G.uart_rx.cnt++] = usart_data_receive(USART5);    
    }
	//若接收检测到空闲帧中断
	if(RESET != usart_flag_get(USART5, USART_FLAG_IDLE))
	{
		usart_data_receive(USART5); // 读寄存器自动清除标志	
		Uart_4G.uart_rx.idle_flg = true;
    }
}


/*!
    \brief      PE8--TX  PE7--RX ( For rs232 LCD)
    \param[in]  baudrate: 
    \param[out] none
    \retval     none
*/
void BSP_Uart6_Config(uint32_t baudrate)
{
	/* enable gpio UART6 clock */
    rcu_periph_clock_enable(RCU_GPIOE);
	rcu_periph_clock_enable(RCU_UART6);

	/* connect port to USARTx_Tx Rx*/
	gpio_af_set(GPIOE, GPIO_AF_8, GPIO_PIN_7 | GPIO_PIN_8);

	/* configure USART Tx Rx as alternate function push-pull */
	gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7|GPIO_PIN_8);
	gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7|GPIO_PIN_8);

	usart_deinit(UART6);
	//usart_sample_bit_config(com, USART_OSB_1bit);
	usart_oversample_config(UART6, USART_OVSMOD_8);
	usart_baudrate_set(UART6,baudrate);				//波特率
	usart_parity_config(UART6, USART_PM_NONE);		//校验位:NONE
	usart_word_length_set(UART6, USART_WL_8BIT);	//数据位:8
	usart_stop_bit_set(UART6, USART_STB_1BIT);		//停止位:1
	usart_receive_config(UART6, USART_RECEIVE_ENABLE);
	usart_transmit_config(UART6, USART_TRANSMIT_ENABLE);
	usart_enable(UART6);
	
    /* enable UART6 transmit interrupt */
    //usart_interrupt_enable(UART6, USART_INT_TBE);
    
	/* enable UARTx receive interrupt */
	usart_interrupt_enable(UART6, USART_INT_RBNE);
	/* enable USARTx IDLEIE interrupt */
	usart_interrupt_enable(UART6, USART_INT_IDLE);

	/* USART interrupt configuration */
	nvic_irq_enable(UART6_IRQn, 6, 0);
	   
}


void UART6_IRQHandler(void)
{
	//若接收到一个字节
	if( RESET != usart_flag_get(UART6, USART_FLAG_RBNE))
    { 
        RS232_LCD.uart_rx.buf[RS232_LCD.uart_rx.cnt++] = (uint8_t)usart_data_receive(UART6);
    }

	//若接收检测到空闲帧中断
	if(RESET != usart_flag_get(UART6, USART_FLAG_IDLE))
	{
		usart_data_receive(UART6); // 读寄存器自动清除标志
		RS232_LCD.uart_rx.idle_flg = true;	
    }
}


// 输出单个字节
void UartX_Put_Char(uint32_t usart_periph, uint8_t ch)
{
    usart_data_transmit(usart_periph, (uint8_t) ch);
    while(RESET == usart_flag_get(usart_periph, USART_FLAG_TBE));
}


/***************************************************************************
* FunctionName   : UartX_Put_String()							   
* Description    : 发送字符串
* EntryParameter : str - 数据缓存; 
* ReturnValue    : None
***************************************************************************/
void UartX_Put_String(uint32_t usart_periph, char *pStr)
{      
    while (*pStr)
    {      
		UartX_Put_Char(usart_periph, *pStr++);
    }
}

// 发送字符数组
void UartX_Put_Array(uint32_t usart_periph, uint8_t *pStr, int Lenth)  
{
    while(Lenth--)   
    {      
        UartX_Put_Char(usart_periph, *pStr++);
    }     
	// rs485 last byte not send out, Must add it here..
	while(RESET == usart_flag_get(usart_periph, USART_FLAG_TC)); 
}

